//
//  SpinButtonView.swift
//  YolanTest
//
//  Created by Fenly on 2018/4/26.
//  Copyright © 2018年 Cotte. All rights reserved.
//

import UIKit

class SpinButtonView: UIView {


    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    @IBOutlet weak var inImgView: UIImageView!
    
    @IBOutlet weak var outImgView: UIImageView!
    
    @IBOutlet weak var characterLb: UILabel!
    
    
    func setupSpinButtonView(text: String, canScale: Bool) {
        characterLb.text = text
        addRotationAnim()
        if canScale {
            addScaleAnim()
        }
    }
    
    private func addRotationAnim() {
        let rotationAnim = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnim.fromValue = 0
        rotationAnim.toValue = Double.pi * 2
        rotationAnim.repeatCount = MAXFLOAT
        rotationAnim.duration = 2
        // 这个属性很重要 如果不设置当页面运行到后台再次进入该页面的时候 动画会停止
        rotationAnim.isRemovedOnCompletion = false
        inImgView.layer.add(rotationAnim, forKey: nil)
        
        let reverseRotationAnim = CABasicAnimation(keyPath: "transform.rotation.z")
        reverseRotationAnim.fromValue = Double.pi * 2
        reverseRotationAnim.toValue = 0
        reverseRotationAnim.repeatCount = MAXFLOAT
        reverseRotationAnim.duration = 2
        reverseRotationAnim.isRemovedOnCompletion = false
        outImgView.layer.add(reverseRotationAnim, forKey: nil)
        
        
    }
    
    private func addScaleAnim () {
        let animation = CABasicAnimation(keyPath: "transform.scale");
        animation.duration = 0.5;
        animation.repeatCount = MAXFLOAT;
        animation.autoreverses = true;
        animation.fromValue = 0.9;
        animation.toValue = 1.1;
        
        inImgView.layer.add(animation, forKey: nil)
        outImgView.layer.add(animation, forKey: nil)
    }
    
}
